const global = getApp().globalData;

const Api = global.Api;
const wxApi = global.wxApi;
const map = {
  // 我的课程
  "1": {
    subed: true,
    conditionsType: 3,
    loadBooks(id, params, callback) {
      Api.getMyBooks(params, data => {
        callback & callback(data.records);
      })
    }
  },
  // 所有课程
  "2": {
    subed: false,
    conditionsType: 1,
    loadBooks(id, params, callback) {
      Api.getSubjectBooks(id, params, data => {
        callback & callback(data.records);
      })
    }
  }
}
Page({
  data: {
    books: [],
    subed: false,
    loadMoreType: 0,
    popviewShow: false,
    conditions: [],
    infoViewShow: false,
    selectedConditions: []
  },
  onLoad: function (options) {
    this.pageName = 'books';
    // 根据类型获取对应实例
    this.pageNo = 1;
    this.master = map[ options.type ];
    this.optionId = options.id || '1';
    this.subed = this.master.subed || false;
    this.setData( {
      subed: this.subed
    })
    let navName = options.name;
    wxApi.setNavTitle(navName)
    this.needRefresh = true;
    
  },
  onShow() {
    if (this.needRefresh) {
      this.init();
      this.needRefresh = false;
    }
  },
  init() {
    this.logger = global.logger;
    this.loadConditions();
    this.refresh();
  },
  onPullDownRefresh() {
    this.refresh( () => {
      wx.stopPullDownRefresh()
    } )
  },
  refresh( fn ) {
    this.pageNo = 1;
    this.loadBooks(this.pageNo, data => {
      fn && fn()
      this.setBooks( data );
    });
  },
  onReachBottom: function () {
    this.loadMore();
  },
  // 更新双向绑定属性
  onUpdateAttr: function (e) {
    this.setData(e.detail);
  },
  /*
  *打开筛选侧边栏
  */
  onOpenSelecter: function () {
    this.setData({
      popviewShow: true
    })
  },
  // 筛选侧边栏确认事件
  onSelecterSure: function (e) {

    this.setData({
      popviewShow: false,
      selectedConditions: e.detail.selected
    });
    this.refresh();
  },
  onBookDetail: function (e) {
    let book = e.currentTarget.dataset.book;
    
    let params = {
      id: book.id,
      name: book.name
    }
    let path = '../book-detail/book-detail';
    wxApi.navigateTo(path, params);
  },
  setBooks( books ) {
    this.setData({
      books: books,
      infoViewShow: !books.length
    })
  },
  /*
  * 加载更多
  */
  loadMore: function () {
    let self = this;
    let books = this.data.books;

    this.pageNo ++;
    // 显示加载更多
    this.setData({
      loadMoreType: 1
    })

    this.loadBooks(this.pageNo, data => {
      if ( data.length ) {
        books = books.concat( data );
        this.setData({
          loadMoreType: 0,
          books: books
        })
      } else {
        self.pageNo --;
        this.setData({
          loadMoreType: 2
        })
      }
    })
  },
  loadBooks: function (pageNo, callback) {
    let params = {
      conditions: this.getSelectedConditions(),
      pageNo: pageNo,
      pageSize: 10
    }
    this.master.loadBooks(this.optionId, params, data => {
      this.logger.info('课本列表', data)
      callback && callback( data );
    });
  },
  setSelectedConditions( conditions ) {
    this.setData({
      selectedConditions: conditions
    })
  },
  getSelectedConditions() {
    return this.data.selectedConditions;
  },
  /*
  * 加载筛选条件列表
  */
  loadConditions() {
    let type = this.master.conditionsType;
    Api.getBookConditions(type, (data) => {
      let conditions = this.packConditions(data);
      this.setData({
        conditions: conditions
      })
    })
  },
  /*
  * 包装筛选条件数组对象，利于后续选中逻辑
  */
  packConditions: function (data) {
    for (let i = 0, len_i = data.length; i < len_i; i++) {

      for (let j = 0, len_j = data[i].options.length; j < len_j; j++) {

        data[i].options[j].active = false;
        data[i].options[j].long = data[i].options[j].name.length > 6
      }
    }
    return data;
  }
})